//主路由
let path = require("path");
let router = require("koa-router")();
let captchaM = require("../middleware/svg-captcha");

let homeRouter = require("./home");
let loginR = require("./login");

let testR = require("./test");
let wechatR = require("./wechat");
let apiR = require("./api");


router.use(async (ctx,next)=>{
    //如果没用登陆状态,则所有页面跳转到login
    //有些是api接口,不需要做这个判断,所以只要进入home时判断即可
    let userInfo = ctx.session.user;
    if(!userInfo && ctx.url=="/home" ){
        ctx.redirect("/login");
    }
    await next();
})
router.get("/captcha",captchaM);
router.use("/home",homeRouter.routes(),homeRouter.allowedMethods());
router.use("/login",loginR.routes(),homeRouter.allowedMethods());
router.use("/test",testR.routes());
router.use("/wechat",wechatR.routes());
router.use("/api",apiR.routes());

//通配路由被调用了,并且有了next执行,这里就会执行,子路由里面没有next也没关系
router.use(async (ctx,next)=>{
    console.log("尾use被调用");
    await next();
})
module.exports = router;